package com.sgb.kjwl.viewmodel.manage;

import android.content.Context;
import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.reflect.TypeToken;
import com.sgb.contract.view.ui.contract.ContractActivity;
import com.sgb.contract.view.ui.openaccount.OpenAccountActivity;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.api.service.WebsiteInfoAPI;
import com.sgb.kjwl.model.entity.LoginEntity;
import com.sgb.kjwl.model.entity.SelfOpenAccountDetailEntity;
import com.sgb.kjwl.model.entity.manage.AnnouncementListEntity;
import com.sgb.kjwl.model.entity.manage.WebsiteBannerEntity;
import com.sgb.kjwl.model.entity.manage.WebsiteConfigureEntity;
import com.sgb.kjwl.model.entity.manage.WebsiteInfoEntity;
import com.sgb.kjwl.model.entity.manage.WebsiteMemberEntity;
import com.sgb.kjwl.model.entity.self.PermissionMenuEntity;
import com.sgb.kjwl.repository.ManageRepertory;
import com.sgb.kjwl.utils.DialogFunction;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.manage.WebsiteInfoActivity;
import com.sgb.kjwl.view.widget.self.permission.BasePermissionCallBack;
import com.sgb.kjwl.view.widget.self.permission.UserPermissionUtils;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.member.entity.MemberBean;
import com.sgb.member.ui.activity.MemberRelieveActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.mmkvutil.CacheMarker;
import com.swgk.core.mmkvutil.DatalUtils;
import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.util.SharedPreferenceUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class WebsiteInfoModel extends BaseViewModel {

    //网站信息
    public MutableLiveData<WebsiteInfoEntity> entityMutableLiveData = new MutableLiveData<>();

    //网站广告页
    public MutableLiveData<ArrayList<WebsiteBannerEntity>> webSiteBanners = new MutableLiveData<>();

    //会员等级
    public MutableLiveData<WebsiteMemberEntity> websiteMember = new MutableLiveData<>();

    private WebsiteInfoActivity activity;
    private ManageRepertory repertory;

    public List<PermissionMenuEntity> list = new ArrayList<>();

    private List<PickSelectEntity> mPopupList;

    public WebsiteInfoModel(WebsiteInfoActivity activity, ManageRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    List<String> mTabNames = new ArrayList<>();
    public List<String> getTabName(int websiteType, List<WebsiteConfigureEntity.NavigateBarEntity> navigateBars, List<Integer> filterMenus, boolean isMySelf){
        String[] mTabName = null;
        mTabNames.clear();
        if (websiteType == 0){
            mTabName = new String[]{"经营业务", "需求公告", "经营业绩", "专项资质", "个人简介",
                    "专项资产", "经营动态", "荣誉荣耀", "知识产权", "宣传展示"};
            if (isMySelf){
                return Arrays.asList(mTabName);
            }
            for (int i = 0; i < mTabName.length; i++) {
                mTabNames.add(mTabName[i]);
            }
            return getReallyTabs(mTabNames, filterMenus);
        } else if (websiteType == 1){
            if (navigateBars == null || navigateBars.isEmpty()) {
                mTabName = new String[]{"经营业务", "需求公告", "经营业绩", "专项资质", "企业简介",
                        "专项资产", "经营动态", "荣誉荣耀", "知识产权", "宣传展示"};
                if (isMySelf){
                    return Arrays.asList(mTabName);
                }
                for (int i = 0; i < mTabName.length; i++) {
                    mTabNames.add(mTabName[i]);
                }
                return getReallyTabs(mTabNames, filterMenus);
            } else {
                for (int i = 0; i < navigateBars.size(); i++) {
                    WebsiteConfigureEntity.NavigateBarEntity entity = navigateBars.get(i);
                    if (entity.getShow()){
                        mTabNames.add(entity.getTitle());
                    }
                }
                if (isMySelf){
                    return mTabNames;
                }
                return getReallyTabs(mTabNames, filterMenus);
            }
        } else if (websiteType == 2){
            mTabName = new String[]{"经营业务", "需求公告", "经营业绩", "专项资质", "团队简介",
                    "专项资产", "经营动态", "荣誉荣耀", "知识产权", "宣传展示"};
            if (isMySelf){
                return Arrays.asList(mTabName);
            }
            for (int i = 0; i < mTabName.length; i++) {
                mTabNames.add(mTabName[i]);
            }
            return getReallyTabs(mTabNames, filterMenus);
        } else if (websiteType == 3) {
            mTabName = new String[]{"经营业务", "需求公告", "经营业绩", "专项资质",  "政府简介",
                    "专项资产", "经营动态", "荣誉荣耀", "知识产权", "宣传展示"};
            if (isMySelf){
                return Arrays.asList(mTabName);
            }
            for (int i = 0; i < mTabName.length; i++) {
                mTabNames.add(mTabName[i]);
            }
            return getReallyTabs(mTabNames, filterMenus);
        }
        return null;
    }

    public List<String> getReallyTabs(List<String> all, List<Integer> filterMenus){
        if (filterMenus == null || filterMenus.isEmpty()){
            return all;
        }
        for (Integer index : filterMenus){
            if (index == 1 && all.contains("经营业务")){
                all.remove("经营业务");
            }
            if (index == 2 && all.contains("需求公告")){
                all.remove("需求公告");
            }
            if (index == 3 && all.contains("会员经营")){
                all.remove("会员经营");
            }
            if (index == 4 && all.contains("经营业绩")){
                all.remove("经营业绩");
            }
            if (index == 5 && all.contains("专项资质")){
                all.remove("专项资质");
            }
            if (index == 6 && all.contains("专项资产")){
                all.remove("专项资产");
            }
            if (index == 7 && all.contains("经营动态")){
                all.remove("经营动态");
            }
            if (index == 8 && all.contains("荣誉荣耀")){
                all.remove("荣誉荣耀");
            }
            if (index == 9 && all.contains("知识产权")){
                all.remove("知识产权");
            }
            if (index == 10 && all.contains("求职招聘")){
                all.remove("求职招聘");
            }
            if (index == 11 && all.contains("宣传展示")){
                all.remove("宣传展示");
            }
            if (index == 12 && all.contains("简介")){
                all.remove("简介");
            }
        }
        return all;
    }

    //-----网站详情----------------------------------------------------------------
    public void getWebsiteInfo(String siteNo, int siteType) {
        Map<String, String> map = new HashMap();
        map.put("siteNo", siteNo);
        map.put("siteType", siteType + "");
//        if (siteType == 0){
//            getDetail();
//        }else if (siteType == 1){
        getWebsiteInfo(map);
//        }
    }


    //-----个人网站详情----------------------------------------------------------------
    public void getDetail() {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getWebsiteInfoAPI().getDetail().enqueue(new ICallBack<BaseEntity<WebsiteInfoEntity>>() {
            @Override
            public void onSuccess(BaseEntity<WebsiteInfoEntity> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showWebsiteInfo(data.getData());
                }
            }
        });
    }

    //-----企业网站详情----------------------------------------------------------------
    public void getWebsiteInfo(Map<String, String> map) {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getWebsiteInfoAPI().getSiteInfo(map).enqueue(new ICallBack<BaseEntity<WebsiteInfoEntity>>() {
            @Override
            public void onSuccess(BaseEntity<WebsiteInfoEntity> data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    if (data.getData() == null){
                        MToast.showToast(activity,TextUtils.isEmpty(data.getMsg())?"当前未查询到网站详情":data.getMsg());
                    }
                    activity.showWebsiteInfo(data.getData());
                }
            }

            @Override
            public void onFail() {
                super.onFail();
            }
        });
    }



    //-----是否属于当前公司----------------------------------------------------------------
    public void isBelongCurrentCompany(String siteNo, int webSiteType) {
        Map<String, String> map = new HashMap();
        map.put("companyNo", siteNo);
        map.put("siteType", webSiteType + "");
        APIManager.getInstance().getWebsiteInfoAPI().isBelongCurrentCompany(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showBottomTab((Boolean) data.getData());
                }
            }
        });
    }

    /**
     * 团队网站 是否属于当前团队
     * @param siteNo
     *
     */

    public void isBelongCurrentGroup(String siteNo) {
        Map<String, String> map = new HashMap();
        map.put("siteNo", siteNo);
        APIManager.getInstance().getWebsiteInfoAPI().isBelongCurrentGroup(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showBottomTab((Boolean) data.getData());
                }
            }
        });
    }

    //-----用户查询是否关注企业----------------------------------------------------------------
    public void getUserFocusCompanyStatus(String siteNo, int siteType) {
        Map<String, String> map = new HashMap();
        map.put("siteNo", siteNo);
        map.put("siteType", siteType + "");
        APIManager.getInstance().getWebsiteInfoAPI().getUserFocusCompanyStatus(WebsiteInfoAPI.UserCenterUrl.URL_USER_FOCUS_COMPANY_STATUS, map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    activity.isShowAttention((Boolean) data.getData());
                }
            }
        });
    }

    //-----关注状态----------------------------------------------------------------
    public void attentionCompanyStatus(String siteNo, int siteType) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map<String, String> map = new HashMap();
        map.put("siteNo", siteNo);
        map.put("siteType", siteType + "");
        APIManager.getInstance().getWebsiteInfoAPI().userFocusCompany(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    activity.attentionShow((Boolean) data.getData());
                }
            }
        });
    }

    //获取网站广告列表
    public void getWebsiteBanner(String websiteNo, int siteType) {
        APIManager.getInstance().getWebsiteInfoAPI().getWebSiteBanner(websiteNo, "" + siteType).enqueue(new ICallBack<BaseEntity<ArrayList<WebsiteBannerEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<ArrayList<WebsiteBannerEntity>> data) {
                if (data != null && data.getData() != null){
                    webSiteBanners.postValue(data.getData());
                } else {
                    webSiteBanners.postValue(new ArrayList<>());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ArrayList<WebsiteBannerEntity>>> call, Throwable t) {
                super.onFailure(call, t);
                webSiteBanners.postValue(new ArrayList<>());
            }
        });

    }
    //获取网站会员关系
    public void getWebsiteMember(String websiteNo) {
        APIManager.getInstance().getWebsiteInfoAPI().getMemberRelation(websiteNo).enqueue(new ICallBack<BaseEntity<WebsiteMemberEntity>>() {
            @Override
            public void onSuccess(BaseEntity<WebsiteMemberEntity> data) {
                if (data != null && data.getData() != null){
                    websiteMember.postValue(data.getData());
                } else {
                    websiteMember.postValue(new WebsiteMemberEntity());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<WebsiteMemberEntity>> call, Throwable t) {
                super.onFailure(call, t);
                websiteMember.postValue(new WebsiteMemberEntity());
            }
        });

    }

    //-----权限控制----------------------------------------------------------------
    public void getPermissionMenu(int websiteType) {
        //DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        String companyNo;
        if (websiteType == 0){
            companyNo = "0"; //
        } else {
            companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        }
        if (!TextUtils.isEmpty(companyNo)){
            getBottomPermission(companyNo, websiteType);
        }
//            APIManager.getInstance().getUserAPI().getPermissionMenu(companyNo, websiteType).enqueue(new ICallBack<BaseEntity<List<PermissionMenuEntity>>>() {
//                @Override
//                public void onSuccess(BaseEntity<List<PermissionMenuEntity>> data) {
//                    if (data.getState().equals(APIManager.RESPONSE_OK)) {
//                        Gson gson = new Gson();
//                        String s = gson.toJson(data.getData(), new TypeToken<List<PermissionMenuEntity>>() {
//                        }.getType());
//                        List<PermissionMenuEntity> permissionMenuEntityList = data.getData();
//                        list = new ArrayList<>();
//                        if (data != null && permissionMenuEntityList.size() != 0) {
//                            for (int i = 0; i < permissionMenuEntityList.size(); i++) {
//                                list.add(permissionMenuEntityList.get(i));
//                            }
//                        }
//                    }
//                }
//            });

    }

    //-----Tab数据源----------------------------------------------------------------
    public List<PickSelectEntity> getPopupList(List<String> tabNames) {
        mPopupList = new ArrayList<>();
        PickSelectEntity entity;
        for (int i = 0; i < tabNames.size(); i++) {
            entity = new PickSelectEntity();
            if (i == 0){
                entity.setCheck(true);
            }
            entity.setProjectscale(tabNames.get(i));
            mPopupList.add(entity);
        }
        return mPopupList;
    }

    /**
     * 获取审批流程状态
     *
     * @param approveType 邀请加盟审批类型 inviteJoinCompany
     *                    申請加盟审批类型 applyJoinCompany
     *                    会员企业调级 memberEvaluate
     *                    会员解除申请 memberRelieve
     */
    public void getProcessStatus(WebsiteInfoActivity activity, int pos, String approveType, MemberBean bean) {
        String comNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        APIManager.getInstance().getWebsiteInfoAPI().getProcessStatus(comNo, "memberRelieve").enqueue(new ICallBack<BaseEntity<Object>>(true, true) {
            @Override
            public void onSuccess(BaseEntity<Object> data) {
                if (data != null) {
                    // "status": "1" //1 表示有审批 0 代表无审批
                    String status = ((Map<String, String>) data.getData()).get("status");
                    if (!TextUtils.isEmpty(status)){
                        MemberRelieveActivity.start(activity, bean, pos,status);
                        // 无审批时弹出确认弹窗,
//                                MemberCommonDialog dialog;
//                        if ("0".equals(status)) {
//                            dialog = new MemberCommonDialog(new MemberCommonDialog.SimpleListener() {
//                                @Override
//                                public void onPositive(Object... objects) {
//                                    // 会员解除申请接口
//                                    relieveMember(activity, pos, bean);
//                                }
//                            }, MemberCommonDialog.STYLE_DISSOLUTION);
//                        } else {
//                            // 有审批时跳转至会员解除申请页
//                            dialog = new MemberCommonDialog(new MemberCommonDialog.SimpleListener() {
//                                @Override
//                                public void onPositive(Object... objects) {
//                                    MemberRelieveActivity.start(activity, bean);
//                                }
//                            }, MemberCommonDialog.STYLE_DISSOLUTION_APPLY);
//                        }
//                        dialog.show(activity, "MemberCommonDialog");
                    } else {
                        MToast.showToast(activity, "网络异常，请稍后重试");
                    }
                }
            }
        });
    }

    // 解除会员申请
    public void relieveMember(WebsiteInfoActivity activity, int pos, MemberBean bean) {
        APIManager.getInstance().getWebsiteInfoAPI().relieveMember(bean.getNo()).enqueue(new ICallBack<BaseEntity<BaseEntity>>(true, true) {
            @Override
            public void onSuccess(BaseEntity<BaseEntity> data) {
                MToast.showToast(activity, "解除会员成功");
                activity.removeMember();
            }
        });
    }

    /**
     *
     * @param type 0--个人 1--企业 2--团队
     * @return
     */
    PermissionMenuEntity.ListEntity listEntity;

    //底部操作菜单
    List<PermissionMenuEntity.ListEntity> mApprovalAgentList = new ArrayList<>();

    /**
     *
     * @param type 网站类型
     * @param isMySelf 是不是自己
     * @param memberEntity 会员关系信息
     */
    public void getBottomMenu(int type, boolean isMySelf, WebsiteMemberEntity memberEntity) {
        mApprovalAgentList.clear();
        if (isMySelf) {
            if (type == 1) {
                listEntity = new PermissionMenuEntity.ListEntity();
                listEntity.setIcon("daiban_grey");
                listEntity.setTitle("待办");
                listEntity.setName("1_daiban");
                mApprovalAgentList.add(listEntity);
            }
        }
        if (isMySelf) {
            if (type == 0 || type == 1) {
                listEntity = new PermissionMenuEntity.ListEntity();
                listEntity.setIcon("dingdan_grey");
                listEntity.setTitle("订单");
                listEntity.setName("7_dingdan");
                mApprovalAgentList.add(listEntity);
            }
        }
        if (isMySelf) {
            if (type == 1) {
                listEntity = new PermissionMenuEntity.ListEntity();
                listEntity.setIcon("fukuan_grey");
                listEntity.setTitle("付款");
                listEntity.setName("8_fukuan");
                listEntity.setCount(0);
                mApprovalAgentList.add(listEntity);
            }
        }
        if (!isMySelf){
            if (type == 1 || type == 0 || type == 2 || type == 3) {
                listEntity = new PermissionMenuEntity.ListEntity();
                listEntity.setIcon("jiaoliu_grey");
                listEntity.setTitle("交流");
                listEntity.setName("20_jiaoliu");
                if (type == 0 || type == 2){
                    mApprovalAgentList.add(0, listEntity);
                } else {
                    mApprovalAgentList.add(listEntity);
                }
            }
        }

        if (!isMySelf){
            if (type == 1) {
                listEntity = new PermissionMenuEntity.ListEntity();
                listEntity.setIcon("jiameng_grey");
                listEntity.setTitle("邀请");
                listEntity.setName("18_jiameng");
                mApprovalAgentList.add(listEntity);
            }
        }

        if (isMySelf) {
            if (type == 1) {
                listEntity = new PermissionMenuEntity.ListEntity();
                listEntity.setIcon("shenqing_grey");
                listEntity.setTitle("申请");
                listEntity.setName("11_shenqing");
                mApprovalAgentList.add(listEntity);
            }
        }

        listEntity = new PermissionMenuEntity.ListEntity();
        listEntity.setIcon("hanxun_grey");
        listEntity.setTitle("函询");
        listEntity.setName("5_hanxun");
        mApprovalAgentList.add(listEntity);


        if (isMySelf) {
            listEntity = new PermissionMenuEntity.ListEntity();
            listEntity.setIcon("jiandu_grey");
            listEntity.setTitle("监督");
            listEntity.setName("9_jiandu");
            mApprovalAgentList.add(listEntity);
        }

        if (isMySelf) {
            if (type == 1) {
                listEntity = new PermissionMenuEntity.ListEntity();
                listEntity.setIcon("hetong_grey");
                listEntity.setTitle("合同");
                listEntity.setName("16_hetong");
                mApprovalAgentList.add(listEntity);
            }
        }
        if (type != 3) {
            listEntity = new PermissionMenuEntity.ListEntity();
            listEntity.setIcon("xunjia_grey");
            listEntity.setTitle("询价");
            listEntity.setName("10_xunjia");
            if (type == 1) {
                mApprovalAgentList.add(listEntity);
            }
            if ((type == 0 && isMySelf)){
                mApprovalAgentList.add(listEntity);
            }
            if ((type == 2 && isMySelf)){
                mApprovalAgentList.add(0, listEntity);
            }
        }

        if (!isMySelf) {
            if (type == 1) {
                listEntity = new PermissionMenuEntity.ListEntity();
                listEntity.setIcon("shenqing_grey");
                listEntity.setTitle("加盟");
                listEntity.setName("11_shenqing");
                mApprovalAgentList.add(listEntity);
            }
        }

        if (!isMySelf){
            if (type == 1 || type == 0 || type == 2 || type == 3) {
                listEntity = new PermissionMenuEntity.ListEntity();
                listEntity.setIcon("guanzhu_grey");
                listEntity.setTitle("关注");
                listEntity.setName("19_guanzhu");
                mApprovalAgentList.add(1, listEntity);
            }
        }

        if (!isMySelf){
            if ((type == 1 || type == 0)
                    && (SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 1) == 1
                    || SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 1) == 0)) {
                //会员关系只有企业个人有
                if (TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
                    listEntity = new PermissionMenuEntity.ListEntity();
                    listEntity.setIcon("tiaojiachajia_grey");
                    listEntity.setTitle("调价/查价");
                    listEntity.setName("22_tiaojiachajia");
                    mApprovalAgentList.add(listEntity);
                } else if (memberEntity != null){
                    if (memberEntity.getDemand()) {//需方会员
                        listEntity = new PermissionMenuEntity.ListEntity();
                        listEntity.setIcon("tiaojiachajia_grey");
                        listEntity.setTitle("调价");
                        listEntity.setName("24_tiaojia");
                        mApprovalAgentList.add(listEntity);
                    }
                    if (memberEntity.getSupply()){//供方会员
                        listEntity = new PermissionMenuEntity.ListEntity();
                        listEntity.setIcon("chajia_grey");
                        listEntity.setTitle("查价");
                        listEntity.setName("25_chajia");
                        mApprovalAgentList.add(listEntity);
                    }
                }
            }
        }
        if (!isMySelf){
            if ((type == 1 || type == 0)
                    && (SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 1) == 1
                    || SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 1) == 0)) {//会员关系只有企业个人有
                if (TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))
                        || (memberEntity != null && (memberEntity.getSupply() || memberEntity.getDemand()))) {
                    listEntity = new PermissionMenuEntity.ListEntity();
                    listEntity.setIcon("tiaoji_grey");
                    listEntity.setTitle("调级");
                    listEntity.setName("21_tiaoji");
                    mApprovalAgentList.add(listEntity);
                }
            }
        }

        if (!isMySelf){
            if ((type == 1 || type == 0)
                    && (SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 1) == 1
                    || SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 1) == 0)) {//会员关系只有企业个人有
                if (TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))
                        || (memberEntity != null && (memberEntity.getSupply() || memberEntity.getDemand()))) {
                    listEntity = new PermissionMenuEntity.ListEntity();
                    listEntity.setIcon("jiechu_grey");
                    listEntity.setTitle("解除");
                    listEntity.setName("23_jiechu");
                    mApprovalAgentList.add(listEntity);
                }
            }
        }

        if (isMySelf) {
            if (type == 1) {
                listEntity = new PermissionMenuEntity.ListEntity();
                listEntity.setIcon("jingjia_grey");
                listEntity.setTitle("竞价");
                listEntity.setName("12_jingjia");
                listEntity.setCount(0);
                mApprovalAgentList.add(listEntity);
            }
        }

        if (isMySelf) {
            if (type == 1) {
                listEntity = new PermissionMenuEntity.ListEntity();
                listEntity.setIcon("jicai_grey");
                listEntity.setTitle("集采");
                listEntity.setName("13_jicai");
                listEntity.setCount(0);
                mApprovalAgentList.add(listEntity);
            }
        }

        activity.renderBottomView(mApprovalAgentList);
    }


    //如果是自己获取菜单权限
    public void getBottomPermission(String websiteNo, int websiteType) {
        List<PermissionMenuEntity> data = DatalUtils.getInstance().getData(MkvUtils.CacheUserCenterMenu, CacheMarker.USER_CENTER_MENU + websiteNo, new TypeToken<List<PermissionMenuEntity>>() {});
        if (data != null){
            list.clear();
            list.addAll(data);
            getOrderMenus(data);
        } else {
            getPermissionMenu(websiteNo, websiteType);
        }
    }

    /**
     * 获取菜单数据
     */
    public void getPermissionMenu(String websiteNo, int websiteType) {
        APIManager.getInstance().getUserAPI().getPermissionMenu(websiteNo, websiteType).enqueue(new ICallBack<BaseEntity<List<PermissionMenuEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PermissionMenuEntity>> data) {
                if (data != null && data.getData() != null){
                    List<PermissionMenuEntity> datas = data.getData();
                    List<PermissionMenuEntity> reallylist = new ArrayList<>();
                    reallylist.addAll(datas);
                    getOrderMenus(reallylist);
                    list.clear();
                    list.addAll(datas == null ? new ArrayList<>() : datas);
                }
            }
        });
    }

    //付款订单
    public MutableLiveData<WebsiteConfigureEntity> mWebsiteConfigureEntity = new MutableLiveData<>();

    //网站配置
    Map<String, Object> map = new HashMap();
    public void getWebsiteConfigure(String siteNo, int siteType){
        map.put("siteNo", siteNo);
        map.put("siteType", siteType + "");
        APIManager.getInstance().getWebsiteInfoAPI().getWebsiteConfigure(map).enqueue(new ICallBack<BaseEntity<WebsiteConfigureEntity>>(true, true) {
            @Override
            public void onSuccess(BaseEntity<WebsiteConfigureEntity> data) {
                if (data != null && data.getData() != null){
                    mWebsiteConfigureEntity.postValue(data.getData());
                } else {
                    mWebsiteConfigureEntity.postValue(new WebsiteConfigureEntity());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<WebsiteConfigureEntity>> call, Throwable t) {
                super.onFailure(call, t);
                mWebsiteConfigureEntity.postValue(new WebsiteConfigureEntity());
            }
        });
    }

    //一级菜单需要过滤的
    public MutableLiveData<List<Integer>> mJudgeFirstMenus = new MutableLiveData<>();
    public void getJudgeFirstMenuData(String siteNo, int siteType){
        map.clear();
        map.put("siteNo", siteNo);
        map.put("siteType", siteType + "");
        APIManager.getInstance().getWebsiteInfoAPI().judgeFirstMenuData(map).enqueue(new ICallBack<BaseEntity<List<Integer>>>() {
            @Override
            public void onSuccess(BaseEntity<List<Integer>> data) {
                if (data.getData() != null){
                    mJudgeFirstMenus.postValue(data.getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<Integer>>> call, Throwable t) {
                super.onFailure(call, t);
                mJudgeFirstMenus.postValue(new ArrayList());
            }
        });
    }


    //付款订单
    public MutableLiveData<List<PickFilterEntity<Object>>> mPayMenusLiveData = new MutableLiveData<>();

    //审核订单
    public MutableLiveData<List<PickFilterEntity<Object>>> mCheckMenusLiveData = new MutableLiveData<>();


    public MutableLiveData<List<PickFilterEntity<ArrayList<PickFilterEntity<Object>>>>> mMenusLiveData = new MutableLiveData<>();


    //集采菜单
    public MutableLiveData<List<PickFilterEntity<ArrayList<PickFilterEntity<Object>>>>> mCentralizedMiningLiveData = new MutableLiveData<>();

    //询价--商品
    public MutableLiveData<List<PickFilterEntity<Object>>> mEnquiryShopLiveData = new MutableLiveData<>();

    //企业订单全选
    public void getOrderMenus(List<PermissionMenuEntity> data) {
        UserPermissionUtils.getInstance().getPermission(data, new BasePermissionCallBack() {
            @Override
            public void getOrderPermission(List<PickFilterEntity<ArrayList<PickFilterEntity<Object>>>> orderMenus) {
                if (orderMenus != null){
                    mMenusLiveData.postValue(orderMenus);
                }
            }

            @Override
            public void getPayPermission(List<PickFilterEntity<Object>> payMenus) {
                if (payMenus != null){
                    mPayMenusLiveData.postValue(payMenus);
                }
            }

            @Override
            public void getCheckPermission(List<PickFilterEntity<Object>> checkMenus) {
                if (checkMenus != null){
                    mCheckMenusLiveData.postValue(checkMenus);
                }
            }

            @Override
            public void getCentralizedMiningPermission(List<PickFilterEntity<ArrayList<PickFilterEntity<Object>>>> centralizedMiningMenus) {
                if (centralizedMiningMenus != null){
                    mCentralizedMiningLiveData.postValue(centralizedMiningMenus);
                }
            }

            @Override
            public void getShopEnquiryPermission(List<PickFilterEntity<Object>> shopEnquiryMenus) {
                if (shopEnquiryMenus != null){
                    mEnquiryShopLiveData.postValue(shopEnquiryMenus);
                }
            }
        });
    }

    /**
     *
     * @param isMyself 是不是自己
     * @param websiteMemberEntity 会员关系
     * @param mConfigureEntity 配置信息
     */
    List<WebsiteConfigureEntity.MenuEntity> reallyMenus = new ArrayList<>();
    public void getCompanyBottomMenus(int websiteType, boolean isMyself, WebsiteMemberEntity memberEntity, WebsiteConfigureEntity mConfigureEntity) {
        int type = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 1);//本地用户身份
        mApprovalAgentList.clear();
        reallyMenus.clear();
        if (isMyself){
            reallyMenus.addAll(mConfigureEntity == null ? new ArrayList<>() : mConfigureEntity.getInternally());
        } else {
            reallyMenus.addAll(mConfigureEntity == null ? new ArrayList<>() : mConfigureEntity.getForeign());
        }
        if (reallyMenus.isEmpty()){
            getBottomMenu(websiteType, isMyself, memberEntity);
            return;
        }
        for (WebsiteConfigureEntity.MenuEntity entity : reallyMenus){
            if (isMyself) {
                if (entity.getKey() == 1 && entity.getChecked()) {//代办
                    listEntity = new PermissionMenuEntity.ListEntity();
                    listEntity.setIcon("daiban_grey");
                    listEntity.setTitle("待办");
                    listEntity.setName("1_daiban");
                    mApprovalAgentList.add(listEntity);
                }
                if (entity.getKey() == 2 && entity.getChecked()) {
                    listEntity = new PermissionMenuEntity.ListEntity();
                    listEntity.setIcon("dingdan_grey");
                    listEntity.setTitle("订单");
                    listEntity.setName("7_dingdan");
                    mApprovalAgentList.add(listEntity);
                }
                if (entity.getKey() == 4 && entity.getChecked()) {
                    listEntity = new PermissionMenuEntity.ListEntity();
                    listEntity.setIcon("fukuan_grey");
                    listEntity.setTitle("付款");
                    listEntity.setName("8_fukuan");
                    mApprovalAgentList.add(listEntity);
                }
                if (entity.getKey() == 6 && entity.getChecked()) {
                    listEntity = new PermissionMenuEntity.ListEntity();
                    listEntity.setIcon("shenqing_grey");
                    listEntity.setTitle("申请");
                    listEntity.setName("11_shenqing");
                    mApprovalAgentList.add(listEntity);
                }
                if (entity.getKey() == 7 && entity.getChecked()) {
                    listEntity = new PermissionMenuEntity.ListEntity();
                    listEntity.setIcon("hanxun_grey");
                    listEntity.setTitle("函询");
                    listEntity.setName("5_hanxun");
                    mApprovalAgentList.add(listEntity);
                }
                if (entity.getKey() == 8 && entity.getChecked()) {
                    listEntity = new PermissionMenuEntity.ListEntity();
                    listEntity.setIcon("jiandu_grey");
                    listEntity.setTitle("监督");
                    listEntity.setName("9_jiandu");
                    mApprovalAgentList.add(listEntity);
                }
                if (entity.getKey() == 3 && entity.getChecked()) {
                    listEntity = new PermissionMenuEntity.ListEntity();
                    listEntity.setIcon("hetong_grey");
                    listEntity.setTitle("合同");
                    listEntity.setName("16_hetong");
                    mApprovalAgentList.add(listEntity);
                }
                if (entity.getKey() == 5 && entity.getChecked()) {
                    listEntity = new PermissionMenuEntity.ListEntity();
                    listEntity.setIcon("xunjia_grey");
                    listEntity.setTitle("询价");
                    listEntity.setName("10_xunjia");
                    mApprovalAgentList.add(listEntity);
                }
                if (entity.getKey() == 9 && entity.getChecked()) {
                    listEntity = new PermissionMenuEntity.ListEntity();
                    listEntity.setIcon("jingjia_grey");
                    listEntity.setTitle("竞价");
                    listEntity.setName("12_jingjia");
                    mApprovalAgentList.add(listEntity);
                }
                if (entity.getKey() == 10 && entity.getChecked()) {
                    listEntity = new PermissionMenuEntity.ListEntity();
                    listEntity.setIcon("jicai_grey");
                    listEntity.setTitle("集采");
                    listEntity.setName("13_jicai");
                    mApprovalAgentList.add(listEntity);
                }
//                if (entity.getKey() == 11 && entity.getChecked()) {//编辑
//                    listEntity = new PermissionMenuEntity.ListEntity();
//                    listEntity.setIcon("bianji_grey");
//                    listEntity.setTitle("编辑");
//                    listEntity.setName("14_bianji");
//                    mApprovalAgentList.add(listEntity);
//                }
            } else {
                if (entity.getKey() == 5 && entity.getChecked()) {//交流
                    listEntity = new PermissionMenuEntity.ListEntity();
                    listEntity.setIcon("jiaoliu_grey");
                    listEntity.setTitle("交流");
                    listEntity.setName("20_jiaoliu");
                    mApprovalAgentList.add(0, listEntity);
                }
                if (type == 1 && entity.getKey() == 2 && entity.getChecked()) {
                    listEntity = new PermissionMenuEntity.ListEntity();
                    listEntity.setIcon("jiameng_grey");
                    listEntity.setTitle("邀请");
                    listEntity.setName("18_jiameng");
                    mApprovalAgentList.add(listEntity);
                }
                if (entity.getKey() == 4 && entity.getChecked()) {
                    listEntity = new PermissionMenuEntity.ListEntity();
                    listEntity.setIcon("hanxun_grey");
                    listEntity.setTitle("函询");
                    listEntity.setName("5_hanxun");
                    mApprovalAgentList.add(listEntity);
                }
                if (entity.getKey() == 6 && entity.getChecked()) {
                    listEntity = new PermissionMenuEntity.ListEntity();
                    listEntity.setIcon("xunjia_grey");
                    listEntity.setTitle("询价");
                    listEntity.setName("10_xunjia");
                    mApprovalAgentList.add(listEntity);
                }
//                if (entity.getKey() == 1 && entity.getChecked()) {
//                    listEntity = new PermissionMenuEntity.ListEntity();
//                    listEntity.setIcon("shenqing_grey");
//                    listEntity.setTitle("加盟");
//                    listEntity.setName("11_shenqing");
//                    mApprovalAgentList.add(listEntity);
//                }
                if (entity.getKey() == 3 && entity.getChecked()) {
                    listEntity = new PermissionMenuEntity.ListEntity();
                    listEntity.setIcon("guanzhu_grey");
                    listEntity.setTitle("关注");
                    listEntity.setName("19_guanzhu");
                    mApprovalAgentList.add(listEntity);

                }
                if (type == 1 && entity.getKey() == 11 && entity.getChecked()) {
                    listEntity = new PermissionMenuEntity.ListEntity();
                    listEntity.setIcon("heyue_grey");
                    listEntity.setTitle("合约");
                    listEntity.setName("26_heyue");
                    mApprovalAgentList.add(listEntity);

                }
                if (entity.getKey() == 12 && entity.getChecked()) {
                    listEntity = new PermissionMenuEntity.ListEntity();
                    listEntity.setIcon("jubao_grey");
                    listEntity.setTitle("举报");
                    listEntity.setName("28_jubao");
                    mApprovalAgentList.add(listEntity);

                }
                if ((type == 0 || type == 1) && entity.getKey() == 13 && entity.getChecked()) {
                    listEntity = new PermissionMenuEntity.ListEntity();
                    listEntity.setIcon("baojia_grey");
                    listEntity.setTitle("报价");
                    listEntity.setName("27_baojia");
                    mApprovalAgentList.add(listEntity);

                }
                if ((entity.getKey() == 8 && entity.getChecked() || entity.getKey() == 9 && entity.getChecked()) && TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))) {
                    listEntity = new PermissionMenuEntity.ListEntity();
                    listEntity.setIcon("tiaojiachajia_grey");
                    listEntity.setTitle("调价/查价");
                    listEntity.setName("22_tiaojiachajia");
                    mApprovalAgentList.add(listEntity);
                } else if ((type == 0 || type == 1) && memberEntity != null){
                    if (memberEntity.getDemand() && entity.getKey() == 8 && entity.getChecked()) {//需方会员
                        listEntity = new PermissionMenuEntity.ListEntity();
                        listEntity.setIcon("tiaojiachajia_grey");
                        listEntity.setTitle("调价");
                        listEntity.setName("24_tiaojia");
                        mApprovalAgentList.add(listEntity);
                    }
                    if (memberEntity.getSupply() && entity.getKey() == 9 && entity.getChecked()){//供方会员
                        listEntity = new PermissionMenuEntity.ListEntity();
                        listEntity.setIcon("tiaojiachajia_grey");
                        listEntity.setTitle("查价");
                        listEntity.setName("25_chajia");
                        mApprovalAgentList.add(listEntity);
                    }
                }
                if (TextUtils.isEmpty(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, ""))
                        || ((type == 0 || type == 1) && memberEntity != null && (memberEntity.getSupply() || memberEntity.getDemand()))) {
                    if (entity.getKey() == 7 && entity.getChecked()) {
                        listEntity = new PermissionMenuEntity.ListEntity();
                        listEntity.setIcon("tiaoji_grey");
                        listEntity.setTitle("调级");
                        listEntity.setName("21_tiaoji");
                        mApprovalAgentList.add(listEntity);
                    }
                    if (entity.getKey() == 10 && entity.getChecked()) {
                        listEntity = new PermissionMenuEntity.ListEntity();
                        listEntity.setIcon("jiechu_grey");
                        listEntity.setTitle("解除");
                        listEntity.setName("23_jiechu");
                        mApprovalAgentList.add(listEntity);
                    }

                }
            }
        }
        activity.renderBottomView(mApprovalAgentList);
    }

    /***
     *  是否开户合同权限判断
     *
     */

    public void getCompanyDetail(Context context) {
        DialogHelper.showProgressDialog2(context, null, "数据加载中", 0, true, null,false);
        NewAPIManager.getInstance().getCommonAPI().getCompanyDetail().enqueue(new Callback<BaseEntity<SelfOpenAccountDetailEntity>>() {
            @Override
            public void onResponse(@NotNull Call<BaseEntity<SelfOpenAccountDetailEntity>> call, @NotNull Response<BaseEntity<SelfOpenAccountDetailEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.isSuccessful() && "ok".equals(response.body().getState())) {
                    if (response.body().getData().status == 1) {
                        ContractActivity.start(context);
                    } else {
                        com.sgb.kjwl.utils.DialogHelper.showDialog(context, "温馨提示", "开通在线合同签约账户，即可使用在线合同签约", "马上开通", new DialogFunction() {
                            @Override
                            public void normalRoundDialog() {
                                OpenAccountActivity.start(context);
                            }
                        });
                    }
                }
            }

            @Override
            public void onFailure(@NotNull Call<BaseEntity<SelfOpenAccountDetailEntity>> call, @NotNull Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(context, t.getMessage());
            }
        });
    }


    public List<PickFilterEntity<Object>> mList = new ArrayList<>();
    public void getEnquiryList(){
        mList.clear();
        mList.add(new PickFilterEntity("0", "商品询价"));
        mList.add(new PickFilterEntity("1", "出租询价"));
        mList.add(new PickFilterEntity("2", "求租询价"));
    }

    public void getInviteList(){
        mList.clear();
        mList.add(new PickFilterEntity("3", "邀请会员"));
        mList.add(new PickFilterEntity("4", "邀请投标"));
        mList.add(new PickFilterEntity("5", "邀请竞价"));
        mList.add(new PickFilterEntity("6", "邀请报价"));
        mList.add(new PickFilterEntity("7", "邀请集采"));
    }

    /**
     * 获取im账号
     *
     */
    //im信息
    public MutableLiveData<LoginEntity> mImMsg = new MutableLiveData();
    public void getImAccount(String siteNo, String siteType){
        APIManager.getInstance().getWebsiteInfoAPI().getImAccount(siteNo, siteType).enqueue(new ICallBack<BaseEntity<LoginEntity>>(true, true) {
            @Override
            public void onSuccess(BaseEntity<LoginEntity> data) {
                if (data != null && data.getData() != null){
                    mImMsg.postValue(data.getData());
                }
            }
        });
    }
    public MutableLiveData<List<AnnouncementListEntity>> mAnnouncementList = new MutableLiveData();
    private HashMap<String, Object> mapAnnouncement = new HashMap<>();
    public void getAnnouncementList(String siteNo, String siteType){
        mapAnnouncement.put("limit", 10);
        mapAnnouncement.put("page", 1);
        mapAnnouncement.put("siteNo", siteNo);
        mapAnnouncement.put("siteType", siteType);
        APIManager.getInstance().getWebsiteInfoAPI().getAnnouncementList(mapAnnouncement).enqueue(new ICallBack<BaseEntity<ListResponse<List<AnnouncementListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<AnnouncementListEntity>>> data) {
                 if (data != null && data.getData() != null && data.getData().getList() != null){
                     mAnnouncementList.postValue(data.getData().getList());
                 } else {
                     mAnnouncementList.postValue(new ArrayList<>());
                 }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<AnnouncementListEntity>>>> call, Throwable t) {
                mAnnouncementList.postValue(new ArrayList<>());
            }
        });
    }


    //跳转h5详情
    public void getH5Url(Context context, String no, String param){
        APIManager.getInstance().getPickAPI().getUrlSetting(param).enqueue(new ICallBack<BaseEntity<H5ReEntity>>(true, true) {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    WebViewX5HostActivity.start(context, data.getData().getUrl() + "?no=" + no );
                } else {
                    MToast.showToast(context, data.getMsg());
                }
            }
        });
    }
}
